#include<bits/stdc++.h>
using namespace std;
int n,m,b,c,mi,h,s;
string st;
struct sb
{
	int a,b;
}sz[2010];
char a;
int main()
{
	freopen("hw.in","r",stdin); 
	freopen("hw.out","w",stdout); 
	mi=2147483647;
	cin>>n>>m;
	cin>>st;
	st=" "+st;
	for(int i=1;i<=n;i++)cin>>a,scanf("%d%d",&b,&c),sz[a].a=b,sz[a].b=c;
	for(int i=1;i<=m;i++)
	{
		h=0;
		s=0;
		if(i==1)
		{
			for(int j=2;j<=m;j++)h+=sz[st[j]].a,s+=sz[st[j]].b;
			mi=min(h,s);
		}
		else if(i==n)
		{
			for(int j=1;j<m;j++)h+=sz[st[j]].a,s+=sz[st[j]].b;
			mi=min(h,s);
		}
		else
		{
			if(i-1>n-i)
			{
				for(int j=1;j<i;j++)
				{
					;
				}
			}
			else
			{
				for(int j=1;j<i;j++);
			}
		}
	}
	cout<<mi;
	
	return 0;
} 
